<?php
class Server
{
	public static $port;
	public static $ip;
	
	static public function set(){
		self::$port = "8080";
		self::$ip = _cfg('ip_server');
	}
	
	static public function getGenres(){
		self::set();
		if(!$data = Cache::get(0,'genres')){
			$data = file_get_contents("http://".self::$ip.":".self::$port."/index.php?mod=genre");
			$data = json_decode($data,true);
			Cache::set($data,0,'genres',999);
		}
		return $data;
	}
	
	static public function getBookInfo($id){
		self::set();
		$url = "http://".self::$ip.":".self::$port."/index.php?mod=books&act=view&id=".intval($id);
		$data = file_get_contents($url);
		$data = json_decode($data,true);
		return $data;
	}	
	
	static public function getBookList($count = 5,$page = 1,$genre = 0){
		self::set();
		$url = "http://".self::$ip.":".self::$port."/index.php?mod=books&act=list&count=".intval($count)."&page=".intval($page)."&genre=".intval($genre);
		if(!$data = Cache::get($url,'book_list')){
			$data = file_get_contents($url);
			$data = json_decode($data,true);
			Cache::set($data,$url,'book_list',999);
		}
		return $data;
	}
	
	static public function getPage($book_id = 1,$page_id = 1){
		self::set();
		$url = "http://".self::$ip.":".self::$port."/index.php?mod=pages&bid=".intval($book_id)."&pid=".intval($page_id);
		if(!$data = Cache::get($url,'page')){
			$data = file_get_contents($url);
			$data = json_decode($data,true);
			Cache::set($data,$url,'page',999);
		}
		return $data;
	}
}